package com.cade.agriculturemall.utils;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.widget.AbsListView;

import com.cade.agriculturemall.view.refresh.OnLoadMoreListener;
import com.cade.agriculturemall.view.refresh.OnRefreshListener;
import com.cade.agriculturemall.view.refresh.RefreshFooter;
import com.cade.agriculturemall.view.refresh.RefreshHeader;
import com.xjy.widget.bounce.CanRefreshLayout;
import com.xjy.widget.bounce.pull.OnPullListener;
import com.xjy.widget.bounce.pull.PullLayout;

/**
 * Created by tom on 17/7/11.
 */

public class RefreshLayout extends PullLayout implements OnPullListener {

    private OnRefreshListener mOnRefreshListener;
    private OnLoadMoreListener mOnLoadMoreListener;

    public RefreshLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setHeaderView(new RefreshHeader(context));
        setFooterView(new RefreshFooter(context));
        setPinContent(false);
        setAutoRefresh(true);
        setOnPullListener(this);
        setReleaseRatio(0.8f);
    }

    @Override
    public boolean canChildViewPullDown() {
        boolean can = canChildScrollUp();
        return !can;
    }

    protected boolean canChildScrollDown() {
        if (android.os.Build.VERSION.SDK_INT < 14) {
            if (getContentView() instanceof AbsListView) {
                final AbsListView absListView = (AbsListView) getContentView();
                return absListView.getChildCount() > 0
                        && (absListView.getLastVisiblePosition() < absListView.getChildCount() - 1
                        || absListView.getChildAt(absListView.getChildCount() - 1).getBottom() > absListView.getPaddingBottom());
            } else {
                return ViewCompat.canScrollVertically(getContentView(), 1) || getContentView().getScrollY() < 0;
            }
        } else {
            return ViewCompat.canScrollVertically(getContentView(), 1);
        }
    }

    @Override
    public boolean canChildViewPullUp() {
        return !canChildScrollDown();
    }

    protected boolean canChildScrollUp() {
        if (android.os.Build.VERSION.SDK_INT < 14) {
            if (getContentView() instanceof AbsListView) {
                final AbsListView absListView = (AbsListView) getContentView();
                return absListView.getChildCount() > 0
                        && (absListView.getFirstVisiblePosition() > 0 || absListView.getChildAt(0)
                        .getTop() < absListView.getPaddingTop());
            } else {
                return ViewCompat.canScrollVertically(getContentView(), -1) || getContentView().getScrollY() > 0;
            }
        } else {

            return ViewCompat.canScrollVertically(getContentView(), -1);
        }
    }

    @Override
    public boolean canChildViewPullLeft() {
        return false;
    }

    @Override
    public boolean canChildViewPullRight() {
        return false;
    }

    @Override
    public void onStartRefreshing() {
        if (mOnRefreshListener != null){
            mOnRefreshListener.onStartRefresh();
        }
    }

    @Override
    public void onStartLoadMore() {
        if (mOnLoadMoreListener != null){
            mOnLoadMoreListener.onStartLoadMore();
        }
    }

    public void setOnRefreshListener(OnRefreshListener onRefreshListener) {
        mOnRefreshListener = onRefreshListener;
    }

    public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
        mOnLoadMoreListener = onLoadMoreListener;
    }
}
